#!/bin/bash
source ~/command/common

if [ -z "$1" -o "$1" = "-h" -o "$1" = "--help" ];then
    echo "Usage:"
    echo "    xfor command: 使用clipboard的内容，如：复制'a b c'执行'xfor \"echo #i#i\"'会显示'aa bb cc'"
    echo "    xfor 10 command: 从0开始"
    echo "    xfor 3 10 command: 从3开始"
    echo "    xfor 3 10 2 command: 从3开始，步长为2，可以为-1"
    echo "    command: \"echo #i\""
    exit
fi
if [ $# = '1' ];then
    if __mac ;then
        str=`pbpaste`
    else
        str=`cat /dev/clipboard`
    fi
    __ask "执行${str}吗"
    if [ "$?" = "$_FALSE_" ];then
        exit
    fi
    str=($str)
    for i in "${str[@]}";do
        ${1//#i/$i}
    done
elif [ $# = '2' ];then
    for ((i=0; i<=$1; i++));do
        ${2//#i/$i}
    done
elif [ $# = '3' ];then
    for ((i=$1; i<=$2; i++));do
        ${3//#i/$i}
    done
elif [ $3 -lt 0 ];then
    for ((i=$1; i>=$2; i+=$3));do
        ${4//#i/$i}
    done
else
    for ((i=$1; i<=$2; i+=$3));do
        ${4//#i/$i}
    done
fi
